//
//  ShieldButtonNode.m
//  iFly
//
//  Created by User-23 on 12/10/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "ShieldButtonNode.h"
#import "Constants.h"
#import "CharacterNode.h"


@implementation ShieldButtonNode

+(instancetype) shieldButtonOnPosition:(CGPoint) position inScene:(SKScene*) scene {
    ShieldButtonNode* button = [self spriteNodeWithImageNamed:shieldButtonImage];
    
    //setting logic for the button
    button.name = @"shield";
    button.position = position;
    button.zPosition = 1;
    button.isButtonClicked = NO;
    button.physicsBody.categoryBitMask = shieldCategory;
    return button;
}

-(void)activateShield {
    
    if (!self.isButtonClicked && !self.parent.isPaused){
        self.isButtonClicked = YES;
        
        CharacterNode* character = (CharacterNode*)[self.parent childNodeWithName:@"character"];
        SKAction* activate = [SKAction runBlock:^{
            [character activateShield];
        }];
        SKAction* wait = [SKAction waitForDuration:kShieldDuration];
        SKAction* deactivate = [SKAction runBlock:^{
            [character deactivateShield];
        }];
        SKAction* shieldCooldown = [SKAction waitForDuration:kShieldCooldown];
        SKAction* unlockShield = [SKAction runBlock:^{
            self.isButtonClicked = NO;
        }];
        
        SKAction* sequence = [SKAction sequence:@[activate, wait, deactivate,shieldCooldown,unlockShield]];
        [self runAction:sequence];
    }
}

@end
